﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RelationExpertSystem
{
    public partial class EditPersonDialog : Form
    {
        private IEnumerable<Person> _allPeople;

        public Person Person { get; set; }
        public bool Changes { get; set; }

        public EditPersonDialog(Person editTarget, IEnumerable<Person> relationOptions, IEnumerable<Person> allPeople)
        {
            InitializeComponent();

            Person = editTarget;
            LoadPersonOptions(relationOptions);
            _allPeople = allPeople;
            LoadPersonInfo();
            Changes = false;
        }

        private void LoadPersonInfo()
        {
            if (Person.IsNull(Person))
                return;

            txtNewName.Text = Person.Name;
            if (Person.Sex == Sex.Female)
                rbNewSexFemale.Checked = true;
            if (!Person.IsNull(Person.Father))
                cbNewFather.SelectedItem = Person.Father;
            if (!Person.IsNull(Person.Mother))
                cbNewMother.SelectedItem = Person.Mother;
            if (!Person.IsNull(Person.Spouse))
                cbNewSpouse.SelectedItem = Person.Spouse;
        }

        private void LoadPersonOptions(IEnumerable<Person> options)
        {
            foreach (Person p in options)
            {
                cbNewFather.Items.Add(p);
                cbNewMother.Items.Add(p);
                cbNewSpouse.Items.Add(p);
            }
        }
        
        private void HandleBtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void HandleBtnFinish_Click(object sender, EventArgs e)
        {
            if (Changes)
            {
                string name = txtNewName.Text.Trim();
                if (String.IsNullOrEmpty(name))
                {
                    MessageBox.Show("Invalid Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (DuplicateName(name))
                {
                    MessageBox.Show("Duplicate names not allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                Sex sex = rbNewSexFemale.Checked ? Sex.Female : Sex.Male;

                if (!Person.IsNull(Person))
                {
                    Person.Name = name;
                    Person.Sex = sex;
                    Person.Mother = cbNewMother.SelectedItem as Person;
                    Person.Father = cbNewFather.SelectedItem as Person;
                    Person.Spouse = cbNewSpouse.SelectedItem as Person;
                }
                else
                {
                    Person = new Person(name, sex, cbNewFather.SelectedItem as Person,
                        cbNewMother.SelectedItem as Person, cbNewSpouse.SelectedItem as Person);
                }
            }
            
            this.DialogResult = DialogResult.OK;
        }

        private bool DuplicateName(string name)
        {
            foreach (Person p in _allPeople)
            {
                if (p == Person)
                    continue;

                if (p.Name == name)
                    return true;
            }

            return false;
        }

        private void HandleNameChanged(object sender, EventArgs e)
        {
            Changes = true;
        }

        private void HandleSexChanged(object sender, EventArgs e)
        {
            Changes = true;
        }

        private void HandleSelectedIndexChanged(object sender, EventArgs e)
        {
            Changes = true;
        }
    }
}
